﻿namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ShippingMethodRepository : IShippingMethodRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IShippingMethodRepository Members

		public int CreateNewShippingMethod(Data.Entity.ShippingMethod theNewShippingMethod)
		{
			if(theNewShippingMethod == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ShippingMethod_Create(theNewShippingMethod.Name, theNewShippingMethod.Description,
					theNewShippingMethod.DisplayOrder, theNewShippingMethod.IsActive);
			}

			return result;
		}

		public IList<Data.Entity.ShippingMethod> GetAllShippingMethod()
		{
			List<ECommerce_GetShippingMethodResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetShippingMethod();

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ShippingMethod>();

				foreach(var sm in lstData)
				{
					var theEntity = new Data.Entity.ShippingMethod()
					{
						Description = sm.Description,
						DisplayOrder = sm.DisplayOrder,
						IsActive = sm.IsActive,
						Name = sm.Name,
						ShippingMethodID = sm.ShippingMethodID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.ShippingStatus> GetAllShippingStatus()
		{
			List<ECommerce_GetShippingStatusResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetShippingStatus();

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ShippingStatus>();

				foreach(var ss in lstData)
				{
					var theEntity = new Data.Entity.ShippingStatus()
					{
						Name = ss.Name,
						ShippingStatusID = ss.ShippingStatusID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public bool DeleteShippingMethod(int id)
		{
			if(id <= 0)
			{
				return false;
			}

			bool result = false;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbShippingMethod = (from sm in _dbContext.ShippingMethods
										where sm.ShippingMethodID.Equals(id)
										select sm).FirstOrDefault();

				if(dbShippingMethod == null)
				{
					result = false;
				}
				else
				{
					_dbContext.ShippingMethods.DeleteOnSubmit(dbShippingMethod);

					_dbContext.SubmitChanges();

					result = true;
				}
			}

			return result;
		}

		public int UpdateShippingMethod(Data.Entity.ShippingMethod theShippingMethod)
		{
			if(theShippingMethod == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbShippingMethod = (from sm in _dbContext.ShippingMethods
										where sm.ShippingMethodID.Equals(theShippingMethod.ShippingMethodID)
										select sm).FirstOrDefault();

				if(dbShippingMethod == null)
				{
					result = 0;
				}
				else
				{
					dbShippingMethod.Description = theShippingMethod.Description;
					dbShippingMethod.DisplayOrder = theShippingMethod.DisplayOrder;
					dbShippingMethod.IsActive = theShippingMethod.IsActive;
					dbShippingMethod.Name = theShippingMethod.Name;

					_dbContext.SubmitChanges();
					result = 1;
				}
			}

			return result;
		}

		public IList<Data.Entity.ShippingMethod> GetShippingMethodByProduct(int productID)
		{
			if(productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetShippingMethod_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetShippingMethod_By_Product(productID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ShippingMethod>();

				foreach(var sm in lstData)
				{
					var theEntity = new Data.Entity.ShippingMethod()
					{
						Description = sm.Description,
						DisplayOrder = sm.DisplayOrder,
						IsActive = sm.IsActive,
						Name = sm.Name,
						ShippingMethodID = sm.ShippingMethodID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public void SaveProduct_ShippingMethod_Mapping(int productID, int shippingMethodID)
		{
			if(productID <= 0 || shippingMethodID <= 0)
			{
				return;
			}

			using(LinqToECommerceDBDataContext db = new LinqToECommerceDBDataContext())
			{
				var dbProduct_ShippingMethod_Mapping = (from psm in db.Product_ShippingMethod_Mappings
														where psm.ProductID.Equals(productID) && psm.ShippingMethodID.Equals(shippingMethodID)
														select psm).FirstOrDefault();

				bool isNew = false;

				if(dbProduct_ShippingMethod_Mapping == null)
				{
					dbProduct_ShippingMethod_Mapping = new Product_ShippingMethod_Mapping();
					isNew = true;
					dbProduct_ShippingMethod_Mapping.ShippingMethodID = shippingMethodID;
					dbProduct_ShippingMethod_Mapping.ProductID = productID;
				}

				if(isNew)
				{
					db.Product_ShippingMethod_Mappings.InsertOnSubmit(dbProduct_ShippingMethod_Mapping);
				}

				db.SubmitChanges();
			}
		}

		public void DeleteTheProduct_ShippingMethod_Mapping(int productID, int shippingMethodID)
		{
			if(productID <= 0 || shippingMethodID <= 0)
			{
				return;
			}

			using(LinqToECommerceDBDataContext db = new LinqToECommerceDBDataContext())
			{
				var dbProduct_ShippingMethod_Mapping = (from ps in db.Product_ShippingMethod_Mappings
														where ps.ProductID.Equals(productID) && ps.ShippingMethodID.Equals(shippingMethodID)
														select ps).FirstOrDefault();

				if(dbProduct_ShippingMethod_Mapping != null)
				{
					db.Product_ShippingMethod_Mappings.DeleteOnSubmit(dbProduct_ShippingMethod_Mapping);

					db.SubmitChanges();
				}
			}
		}

		public IList<Data.Entity.ShippingByStateAndProvince> GetShippingByStateAndProvinceByProvince(int stateProvinceID)
		{
			if(stateProvinceID <= 0)
			{
				return null;
			}

			List<ECommerce_GetShippingByStateAndProvince_By_ProvinceResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetShippingByStateAndProvince_By_Province(stateProvinceID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ShippingByStateAndProvince>();

				foreach(var s in lstData)
				{
					var theEntity = new Data.Entity.ShippingByStateAndProvince()
					{
						ShippingByStateAndProvinceID = s.ShippingByStateAndProvinceID,
						ShippingMethodID = s.ShippingMethodID,
						StateAndProvinceID = s.StateAndProvinceID,
						TransportCosts = s.TransportCosts
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
